const formatComment = comments => {
  if (comments === null) return []
  const commentArr = comments.filter(item => item.comment_id === null)
  commentArr.forEach(item => (item.children = []))
  comments.forEach(item => {
    if (item.comment_id !== null) {
      const index = commentArr.findIndex(i => i.id === item.comment_id)
      if (index !== -1) {
        commentArr[index].children.push(item)
      }
    }
  })
  comments.forEach(item => {
    if (item.comment_id !== null) {
      // 对commentArr 进行遍历，如果item.id 存在 commentArr 某个值的children里面，push进去children
      commentArr.forEach(i => {
        i.children.forEach(e => {
          if (e.id === item.comment_id) {
            i.children.push(item)
          }
        })
      })
    }
  })
  return commentArr
}

export default formatComment
